import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class CountDownLatchDemo {

    //减少
    public static void main(String[] args) throws Exception {

        CountDownLatch countDownLatch = new CountDownLatch(4);
        new Thread(() -> {
            try {
                for (int i = 1; i < 10; i++) {
                    //这里不在外边套线程的话，睡着的就是main，所以到最后mian和9抢
                    //如果不睡的话，4次之后main和其他线程各凭本事
                    //9+main一起的时候，有四次之后main才走，也是抢着走的
                    TimeUnit.SECONDS.sleep(1);
                    //别睡错了
                    new Thread(() -> {
                        System.out.println(Thread.currentThread().getName() + "号同学出去了");
                        countDownLatch.countDown();
                    }, String.valueOf(i)).start();
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }, "a").start();

        countDownLatch.await();
        System.out.println(Thread.currentThread().getName() + "关门");
    }

}
